package com.crane.oss.school.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.ObjectUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.crane.common.core.exception.AppException;
import com.crane.common.core.exception.BasicErrorCode;
import com.crane.common.core.exception.SysException;
import com.crane.common.framework.utils.BeanUtil;
import com.crane.common.framework.utils.DateUtil;
import com.crane.oss.api.bean.resp.TeachCycleResp;
import com.crane.oss.school.bean.request.QueryTeacherCycleReq;
import com.crane.oss.school.bean.request.teachercycle.AddTeacherCycleReq;
import com.crane.oss.school.bean.request.teachercycle.UpdateTeacherCycleReq;
import com.crane.oss.school.bean.resp.TeacherCycleResp;
import com.crane.oss.school.dao.TeacherCycleDao;
import com.crane.oss.school.model.Area;
import com.crane.oss.school.model.TeacherCycle;
import com.crane.oss.school.service.TeacherCycleService;
import com.crane.oss.school.syn.SynXunfeiService;
import com.crane.oss.sports.XunFeiPushEventApiService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;

/**
 * @author zhenxing.pi
 * @email ccsu123456@qq.com
 * @date 2022年03月26日 14时24分54秒
 */
@Service
@Slf4j
public class TeacherCycleServiceImpl implements TeacherCycleService {

    @Resource
    protected TeacherCycleDao teacherCycleDao;

    @Resource
    SynXunfeiService synXunfeiService;

    @Override
    public TeacherCycleResp queryTeacherCycle(Long id) {
        log.info("TeacherCycleBOImpl queryTeacherCycle id={}", id);
        TeacherCycle teacherCycle = teacherCycleDao.selectById(id);
        if (null == teacherCycle) {
            throw new AppException(BasicErrorCode.ENTITY_NOT_EXISTS, "teacherCycle");
        }
        TeacherCycleResp teacherCycleResp = new TeacherCycleResp();
        BeanUtil.copyProperties(teacherCycle, teacherCycleResp);
        return teacherCycleResp;
    }

    @Override
    public IPage<TeacherCycleResp> queryList(QueryTeacherCycleReq queryRequest) {
        log.info("TeacherCycleBOImpl queryList queryRequest={}", queryRequest);
        IPage<TeacherCycleResp> result = new Page<>();
        result.setCurrent(queryRequest.getPageNo());
        result.setPages(queryRequest.getPageSize());

        LambdaQueryWrapper<TeacherCycle> queryTeacherCycleWrapper = new LambdaQueryWrapper<>();
        queryTeacherCycleWrapper.eq(ObjectUtils.isNotNull(queryRequest.getPartyId()), TeacherCycle ::getPartyId, queryRequest.getPartyId());
        queryTeacherCycleWrapper.lt(ObjectUtils.isNotNull(queryRequest.getTime()),TeacherCycle ::getCycleStartTime,queryRequest.getTime());
        queryTeacherCycleWrapper.ge(ObjectUtils.isNotNull(queryRequest.getTime()),TeacherCycle ::getCycleEndTime,queryRequest.getTime());
        IPage<TeacherCycle> page = teacherCycleDao.selectPage(new Page<>(queryRequest.getPageNo(), queryRequest.getPageSize()), queryTeacherCycleWrapper);
        return page.convert(u -> {
            TeacherCycleResp v = new TeacherCycleResp();
            BeanUtils.copyProperties(u, v);
            return v;
        });
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public int save(AddTeacherCycleReq addRequest) {
        log.info("TeacherCycleBOImpl save request={}", addRequest);
        TeacherCycle teacherCycle = new TeacherCycle();
        BeanUtil.copyProperties(addRequest, teacherCycle);
        int i = 0;
        try {
            i = teacherCycleDao.insert(teacherCycle);
            TeachCycleResp teachCycleResp=new TeachCycleResp();
            teachCycleResp.setId(teacherCycle.getId().toString());
            teachCycleResp.setName(teacherCycle.getCycleName());
            teachCycleResp.setBeginDate(DateUtil.format(DateUtil.TIME_PATTERN, teacherCycle.getCycleStartTime()));
            teachCycleResp.setEndDate(DateUtil.format(DateUtil.TIME_PATTERN, teacherCycle.getCycleEndTime()));
            teachCycleResp.setTermNumber(teacherCycle.getSemester());
            synXunfeiService.pushDate(teachCycleResp, XunFeiPushEventApiService.EVENT_TEACH_CYCLE,XunFeiPushEventApiService.INSERT,addRequest.getPartyId().toString(),addRequest.getCreateUserId().toString());
        } catch (Exception e) {
            log.info("TeacherCycleBOImpl add e={}", e);
            throw new SysException();
        }
        return i;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public int update(UpdateTeacherCycleReq updateRequest) {
        log.info("TeacherCycleServiceImpl update request={}", updateRequest);
        TeacherCycle db = teacherCycleDao.selectById(updateRequest.getId());
        if (null == db) {
            throw new AppException(BasicErrorCode.ENTITY_NOT_EXISTS, "teacherCycle");
        }
        if(!db.getPartyId().equals(updateRequest.getPartyId())){
            throw new AppException(BasicErrorCode.NO_OPERATE_RIGHT);
        }
        TeacherCycle teacherCycle = new TeacherCycle();
        BeanUtil.copyProperties(updateRequest, teacherCycle);
        int i = 0;
        try {
            i = teacherCycleDao.updateById(teacherCycle);
            TeachCycleResp teachCycleResp=new TeachCycleResp();
            teachCycleResp.setId(teacherCycle.getId().toString());
            teachCycleResp.setName(teacherCycle.getCycleName());
            teachCycleResp.setBeginDate(DateUtil.format(DateUtil.TIME_PATTERN, teacherCycle.getCycleStartTime()));
            teachCycleResp.setEndDate(DateUtil.format(DateUtil.TIME_PATTERN, teacherCycle.getCycleEndTime()));
            teachCycleResp.setTermNumber(teacherCycle.getSemester());
            synXunfeiService.pushDate(teachCycleResp, XunFeiPushEventApiService.EVENT_TEACH_CYCLE,XunFeiPushEventApiService.UPDATE,db.getPartyId().toString(),db.getCreateUserId().toString());
        } catch (Exception e) {
            log.info("TeacherCycleServiceImpl add e={}", e);
            throw new SysException();
        }
        return i;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public int delete(Long id,Long partyId) {
        log.info("TeacherCycleServiceImpl delete id={}", id);
        TeacherCycle db = teacherCycleDao.selectById(id);
        if (null == db) {
            throw new AppException(BasicErrorCode.ENTITY_NOT_EXISTS, "teacherCycle");
        }
        if(!db.getPartyId().equals(partyId)){
            throw new AppException(BasicErrorCode.NO_OPERATE_RIGHT);
        }
        int i= teacherCycleDao.deleteById(id);
        TeachCycleResp teachCycleResp=new TeachCycleResp();
        teachCycleResp.setId(db.getId().toString());
        teachCycleResp.setName(db.getCycleName());
        teachCycleResp.setBeginDate(DateUtil.format(DateUtil.TIME_PATTERN, db.getCycleStartTime()));
        teachCycleResp.setEndDate(DateUtil.format(DateUtil.TIME_PATTERN, db.getCycleEndTime()));
        teachCycleResp.setTermNumber(db.getSemester());
        synXunfeiService.pushDate(teachCycleResp, XunFeiPushEventApiService.EVENT_TEACH_CYCLE,XunFeiPushEventApiService.DELETE,db.getPartyId().toString(),db.getCreateUserId().toString());
        return i;
    }

}
